package mapspinner;

import java.nio.FloatBuffer;

//this bounding box uses the terrain datatype and produces
//the correct bounding box by means of simple math.
public class TerrainBoundingBox extends com.jme.bounding.BoundingBox {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5789646302692704411L;
	private final TerrainNode terrain;

	public TerrainBoundingBox(final TerrainNode terrain) {
		this.terrain = terrain;
	}

	@Override
	public void computeFromPoints(final FloatBuffer points) {
		final FloatBuffer colorBuf = terrain.getMesh().getColorBuffer();
		float maxY = Float.MIN_VALUE;
		float minY = Float.MAX_VALUE;
		for (int index = 0; index < terrain.getVertexCount(); index++) {
			final float y = colorBuf.get(index * 4 + 3);
			if (y > maxY)
				maxY = y;
			if (y < minY)
				minY = y;
		}

		center.set(1, minY + maxY, 1);
		center.multLocal(0.5f);

		xExtent = 1 - center.x;
		yExtent = maxY - center.y;
		zExtent = 1 - center.z;
	}

}
